package com.yksj.healthtalk.ui.doctorstation.service;

import org.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.graphics.drawable.LevelListDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 
 * 成员管理
 * @author zhao
 *
 */
public class DoctorServiceGroupMangeActivity extends BaseFragmentActivity implements OnClickListener {
	
	JSONObject mJsonObject;
	DataListAdapter mAdapter;
	ListView mListView;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_service_group_manager_layout);
		
		String jsonStr = null;
		if(arg0 != null){
			jsonStr = arg0.getString("mJsonObject");
		}else{
			jsonStr = getIntent().getStringExtra("json");
		}
		
		try{
			mJsonObject = JSON.parseObject(jsonStr);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		initUI();
		initData();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("mJsonObject", mJsonObject.toJSONString());
	}
	
	private void initUI(){
		initTitle();
		setBackBtn("返回",this);
		titleTextV.setText("成员管理");
		setRightBtnYellowBg("添加",this);
		mListView = (ListView)findViewById(android.R.id.list);
		mListView.setAdapter(mAdapter = new DataListAdapter());
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		
		initData();
		
	}
	
	private void initData(){
		HttpRestClient.doHttpQueryGroups(SmartFoxClient.getLoginUserId(),"5", "1",String.valueOf(Integer.MAX_VALUE), "", mJsonObject.getString("PATIENT_GROUP_ID"), new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				JSONArray array = response.getJSONArray("dataList");
				mAdapter.onDataChange(array);
			}
			@Override
			public void onFailure(Throwable error, String content) {
				
			}
		});
	}	
	
	/**
	 * 添加成员
	 */
	private void onDeleteMenber(final JSONObject jsonObject){
//		customerId 我的id patientGroupId 组id otherCustomerIds 需要添加的成员Id，用逗号分隔
		RequestParams params = new RequestParams();
		params.put("customerId", SmartFoxClient.getLoginUserId());
		params.put("patientGroupId","");
		params.put("otherCustomerIds",jsonObject.getString("customerId"));
		HttpRestClient.doHttpAddPatientGroupMember(params, new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);
				if(response.containsKey("error_message")){
					ToastUtil.showShort(response.getString("error_message"));
				}else{
					mAdapter.onDeleteData(jsonObject);
				}
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2://添加成员
			Intent intent = new Intent(this,DoctorServiceGroupMenberAddActivity.class);
			intent.putExtra("json", mJsonObject.toJSONString());
			startActivity(intent);
			break;
		}
	}
	
	
	
	class DataListAdapter extends BaseAdapter{
		JSONArray mArray  = new JSONArray();
		
		public DataListAdapter() {
		}
		
		
		private void onDeleteData(JSONObject object){
			mArray.remove(object);
			notifyDataSetChanged();
		}
		
		private void onDataChange(JSONArray array){
			this.mArray = array;
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mArray.size();
		}

		@Override
		public JSONObject getItem(int arg0) {
			// TODO Auto-generated method stub
			return mArray.getJSONObject(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			final ViewHolder holder;
			final JSONObject jsonObject = getItem(arg0);
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(R.layout.user_info_item_delete,null) ;
				holder.distance = (TextView)convertView.findViewById(R.id.distance);
				holder.head = (ImageView) convertView.findViewById(R.id.head_image);
				holder.personName = (TextView) convertView.findViewById(R.id.name);
				holder.headSex = (ImageView) convertView.findViewById(R.id.head_sex);
				holder.v = (ImageView) convertView.findViewById(R.id.v);
				holder.note = (TextView) convertView.findViewById(R.id.note);
				holder.attentionSalon = (ImageView)convertView.findViewById(R.id.is_friend);
				holder.deleteV = convertView.findViewById(R.id.delete);
				convertView.setTag(holder);
				holder.headSex.setImageDrawable(getResources().getDrawable(R.drawable.sex_drawable));
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			if(!jsonObject.containsKey("display_name")){
				if(jsonObject.getString("customerNickname").length() != 0){
					String name = jsonObject.getString("customerNickname")+"("+jsonObject.getString("customerAccounts")+")";
					jsonObject.put("display_name", name);
				}else{
					jsonObject.put("display_name", jsonObject.getString("customerAccounts"));
				}
			}
			holder.note.setText(jsonObject.getString("personalNarrate"));
			holder.personName.setText(jsonObject.getString("display_name"));
			final LevelListDrawable listDrawable = (LevelListDrawable)holder.headSex.getDrawable();
			listDrawable.setLevel(jsonObject.getIntValue("customerSex"));
			holder.headSex.getDrawable();
			holder.v.setVisibility(CustomerInfoEntity.isShowDoctorV(jsonObject.getIntValue("roleId"))?View.VISIBLE:View.GONE);
			ImageLoader.getInstance().displayImage(
					jsonObject.getString("customerSex"),
					jsonObject.getString("clientIconBackground"),
													holder.head);
			holder.deleteV.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					onDeleteMenber(jsonObject);
				}
			});
			return convertView;
		}
	}
	
	private static  class ViewHolder {
		private TextView note;//描述
		private ImageView head; // 头像
		private TextView personName; // 名称
		private ImageView headSex; // 性别
		private ImageView v;
		private TextView message;//消息的图标
		private ImageView attentionSalon;//是否是关注的好友
		private TextView distance;
		private View deleteV;
	}
	
}
